import {deleteRequest, error, getRequest, ok, postRequest, putRequest} from "../utils/myAxios.ts";
import {Department, Res} from "../types";

export async function getDepList(){
    const list = await getRequest<Array<Department>>(`/department`);
    console.log('获取学院列表',list)
    //判断loginStu的id属性是否存在
    if (list?.length >= 0 ) {
        return {
            code: 200,
            data: list,
            flag: true,
            msg: "ok"
        }
    } else {
        return {
            code: 400,
            data: [],
            flag: false,
            msg: "请求失败"
        }
    }
}


export async function addDepartment(department: Department): Res<Department> {
    const r = await postRequest<Department>(`/department`, department);
    console.log('添加学院：', r);
    if (r.id) {
        return ok(r, '添加成功');
    } else {
        return error(r);
    }
}

export async function updateDepartment(department: Department): Res<Department> {
    const r = await putRequest<Department>(`/department/${department.id}`, department);
    console.log('修改学院res：', r);
    if (r.id) {
        return ok(r, '修改成功');
    } else {
        return error(r);
    }
}

export async function deleteDepartment(id: number): Res<Department> {
    const r = await deleteRequest<Department>(`/department/${id}`);
    console.log('删除学院res：', r);
    if (r.id) {
        return ok(r, '删除成功');
    } else {
        return error(r);
    }
}

//跟据id获取单个
export async function getDepById(id: number): Res<Department> {
    const r = await getRequest<Department>(`/department/${id}`);
    console.log('根据id获取学院res：', r);
    if (r.id) {
        return ok(r, '获取成功');
    } else {
        return error(r);
    }
}